package com.css.dzj.zfjg.modules.admpenalty.controller;

import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.zfjg.common.audit.service.SysAuditService;
import com.css.dzj.zfjg.common.util.*;
import com.css.dzj.zfjg.modules.admpenalty.entity.AdmPenaltyEntity;
import com.css.dzj.zfjg.modules.admpenalty.service.AdmPenaltyService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 行政处罚Controller
 * @Author WT
 * @Date 2020/5/25
 */
@RestController
@RequestMapping("/penalty")
public class AdmPenaltyController {

    @Autowired
    private AdmPenaltyService admPenaltyService;
    // base日志接口
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    PlatformFileUtil platformFileUtil;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Integer pageSize, @RequestParam Integer pageNo){
        String s = "";
        try{
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<AdmPenaltyEntity> admPenaltyResults = admPenaltyService.queryPage(pageRequest);
            s = JSON.toJSONString(admPenaltyResults);
            sysAuditService.saveSuccessAudit("查询行政处罚成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_PAGE, s, request);
            return R.ok().put("data", admPenaltyResults);
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("查询行政处罚失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 信息
     */
    @GetMapping("/info")
    public R info(@RequestParam String id){
        String s ="";
        try {
            AdmPenaltyEntity admPenaltyEntity = admPenaltyService.findById(id);
            s = JSON.toJSONString(admPenaltyEntity);
            sysAuditService.saveSuccessAudit("通过id查询行政处罚成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_SELECT, s, request);
            return R.ok().put("data", admPenaltyEntity);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("通过id查询行政处罚失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_SELECT, s, e, request);
            return R.error(e.getMessage());
        }

    }


    /**
     * 保存提交
     */
    @PostMapping("/save")
    public R save(@RequestBody AdmPenaltyEntity admPenaltyEntity){
        String  s = "";
        try {
            if(StringUtils.isEmpty(admPenaltyEntity)) {
                return R.error("行政处罚信息不能为空");
            }
            admPenaltyService.add(admPenaltyEntity);
            s = JSON.toJSONString(admPenaltyEntity);
            sysAuditService.saveSuccessAudit("添加行政处罚成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_ADD, s, request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("添加行政处罚失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 暂存
     */
    @PostMapping("/storage")
    public R storage(@RequestBody AdmPenaltyEntity admPenaltyEntity){
        String s ="";
        try {
            if(StringUtils.isEmpty(admPenaltyEntity)) {
                return R.error("行政处罚信息不能为空");
            }
            admPenaltyService.storage(admPenaltyEntity);
            s = JSON.toJSONString(admPenaltyEntity);
            sysAuditService.saveSuccessAudit("暂存行政处罚成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_ZCADD, s, request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("暂存行政处罚失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_ZCADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 提交
     */
    @PostMapping("/submit")
    public R submit(@RequestBody AdmPenaltyEntity admPenaltyEntity){
        String s = "";
        try {
            AdmPenaltyEntity oldAdmPenaltyEntity = admPenaltyService.findById(admPenaltyEntity.getId());
            BeanUtil.copyBeanNotNullToBean(admPenaltyEntity, oldAdmPenaltyEntity);
            oldAdmPenaltyEntity.setUpdateTime(new Date());
            oldAdmPenaltyEntity.setTaskStatus(Constant.ISSUBMIT_TRUE);
            admPenaltyService.update(oldAdmPenaltyEntity);
            s =JSON.toJSONString(oldAdmPenaltyEntity);
            sysAuditService.saveSuccessAudit("提交行政处罚成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_SUBMIT, s, request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("提交行政处罚失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_SUBMIT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 退回
     */
    @PostMapping("/back")
    public R back(@RequestBody AdmPenaltyEntity admPenaltyEntity){
        String s = "";
        try {
            AdmPenaltyEntity oldAdmPenaltyEntity = admPenaltyService.findById(admPenaltyEntity.getId());
            BeanUtil.copyBeanNotNullToBean(admPenaltyEntity, oldAdmPenaltyEntity);
            oldAdmPenaltyEntity.setBackTime(new Date());
            oldAdmPenaltyEntity.setTaskStatus(Constant.ISSUBMIT_BACK);
            oldAdmPenaltyEntity.setBackUser(ServiceUtil.getLoginName(request));
            admPenaltyService.update(oldAdmPenaltyEntity);
            s = JSON.toJSONString(oldAdmPenaltyEntity);
            sysAuditService.saveSuccessAudit("退回行政处罚成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_BACK, s, request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("退回行政处罚失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_BACK, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody AdmPenaltyEntity admPenaltyEntity){
        String s = "";
        try {
            AdmPenaltyEntity oldAdmPenaltyEntity = admPenaltyService.findById(admPenaltyEntity.getId());
            BeanUtil.copyBeanNotNullToBean(admPenaltyEntity, oldAdmPenaltyEntity);
            oldAdmPenaltyEntity.setUpdateTime(new Date());
            if(("此案件信息已经本单位领导审定").equals(oldAdmPenaltyEntity.getIsSubmit())){
                oldAdmPenaltyEntity.setIsSubmit("1");
            }else{
                oldAdmPenaltyEntity.setIsSubmit("0");
            }
            admPenaltyService.update(oldAdmPenaltyEntity);
            s = JSON.toJSONString(oldAdmPenaltyEntity);
            sysAuditService.saveSuccessAudit("修改行政处罚成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_UPDATE, s, request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("修改行政处罚失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestParam String id){
        String s = "";
        try {
            if(admPenaltyService.delete(id)){
                s =JSON.toJSONString(id);
                sysAuditService.saveSuccessAudit("删除行政处罚成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_DELETE, s, request);
                return R.ok("操作成功!");
            }else{
                return  R.error("操作失败!");
            }
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("删除行政处罚失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_DELETE, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     *多条件查询行政处罚
     */
    @GetMapping("/xzcfList")
    public R getXzcfist( @RequestParam(required = false) String enforceName,
                         @RequestParam(required = false) String enforceOrganizationSite,
                         @RequestParam(required = false) String enforceObject,
                         @RequestParam(required = false) String breakFact,
                         @RequestParam(required = false) String caseSource,
                         @RequestParam(required = false) String taskStatus,
                         @RequestParam(required = false) String disposeResult,
                         @RequestParam(required = false) String penaltyCategory,
                         @RequestParam(required = false) String enforceOrganization,
                         @RequestParam(required = false) String caseCategory,
                         @RequestParam(required = false) String releaseStartTime,
                         @RequestParam(required = false) String releaseEndTime,
                         @RequestParam(required = false) Integer pageSize, @RequestParam Integer pageNo) {
        String s ="";
        try{
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<AdmPenaltyEntity> admPenaltyResults = admPenaltyService.getXzcfist(enforceName,enforceOrganizationSite,enforceObject,breakFact,
                                                               caseSource,taskStatus,disposeResult,penaltyCategory,enforceOrganization,caseCategory,
                                                                releaseStartTime,releaseEndTime,pageRequest);
            s = JSON.toJSONString(admPenaltyResults);
            sysAuditService.saveSuccessAudit("多条件查询行政处罚成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_PAGE, s, request);
            return R.ok().put("data", admPenaltyResults);
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("多条件查询行政处罚失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 导出选中行数据
     */
    @GetMapping("/exportByIds")
    public R exportHolidayToExcel(HttpServletResponse response, @RequestParam String ids) {
        String s ="";
        try {
            List<AdmPenaltyEntity> admPenaltyEntities = admPenaltyService.queryByIds(ids);
            admPenaltyService.exportAdmPenaltyToExcel(response, admPenaltyEntities);
            s = JSON.toJSONString(admPenaltyEntities);
            sysAuditService.saveSuccessAudit("导出行政处罚成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_EXPORTBYIDS, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("导出行政处罚失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_EXPORTBYIDS, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 根据条件导出数据
     */
    @GetMapping("/export")
    public R export( HttpServletResponse response,
                     @RequestParam(required = false) String enforceName,
                     @RequestParam(required = false) String enforceOrganizationSite,
                     @RequestParam(required = false) String enforceObject,
                     @RequestParam(required = false) String breakFact,
                     @RequestParam(required = false) String caseSource,
                     @RequestParam(required = false) String taskStatus,
                     @RequestParam(required = false) String disposeResult,
                     @RequestParam(required = false) String penaltyCategory,
                     @RequestParam(required = false) String enforceOrganization,
                     @RequestParam(required = false) String caseCategory,
                     @RequestParam(required = false) String releaseStartTime,
                     @RequestParam(required = false) String releaseEndTime) {
        String s = "";
        try{
          List<AdmPenaltyEntity> admPenaltyResults = admPenaltyService.queryByParams(enforceName,enforceOrganizationSite,enforceObject,breakFact,
                                                      caseSource,taskStatus,disposeResult,penaltyCategory,enforceOrganization,caseCategory,
                                                      releaseStartTime,releaseEndTime);
            admPenaltyService.exportAdmPenaltyToExcel(response, admPenaltyResults);
            s  = JSON.toJSONString(admPenaltyResults);
            sysAuditService.saveSuccessAudit("多条件导出行政处罚成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_EXPORTBYIDS, s, request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("多条件导出行政处罚失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_EXPORTBYIDS, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 模板下载
     */
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(){
        String s = "";
        try {
            String fileName = "地震行政处罚案件信息库模板.xls";
            platformFileUtil.download(fileName);
            s = JSON.toJSONString(fileName);
            sysAuditService.saveSuccessAudit("行政处罚模板下载成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_DOWNLOADTEMPLATE, s, request);
        }catch (Exception e){
            sysAuditService.saveFailAudit("行政处罚模板下载失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_DOWNLOADTEMPLATE, s, e, request);


        }
    }

    /**
     *导入
     */
    @PostMapping("/deptExcel")
    public R uploadFile(@RequestParam("file") MultipartFile file,HttpServletResponse response) {
        String s = "";
        try {
            String returnMsg = admPenaltyService.uploadFile(file,response);
            s = JSON.toJSONString(returnMsg);
            sysAuditService.saveSuccessAudit("行政处罚导入成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_DEPTEXCEL, s, request);
            return R.ok().put("data", returnMsg);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("行政处罚导入失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_DEPTEXCEL, s, e, request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 中国局审核行政处罚
     * taskStatus 通过 3 未通过4
     * 未通过添加未通过原因
     * 审核人 审核时间
     */
    @PostMapping("/audit")
    public R audit(@RequestBody AdmPenaltyEntity admPenaltyEntity){
        String s ="";
        try{

            AdmPenaltyEntity oldAdmPenaltyEntity = admPenaltyService.findById(admPenaltyEntity.getId());
            BeanUtil.copyBeanNotNullToBean(admPenaltyEntity, oldAdmPenaltyEntity);
            if(admPenaltyEntity.getTaskStatus().equals("3")){
                oldAdmPenaltyEntity.setTaskStatus(Constant.ISSUBMIT_PASS);
            }else {
                oldAdmPenaltyEntity.setTaskStatus(Constant.ISSUBMIT_NOTPASS);
            }
            oldAdmPenaltyEntity.setAuditUser(ServiceUtil.getLoginName(request));
            oldAdmPenaltyEntity.setAuditOrg(ServiceUtil.getOrgName(request));
            oldAdmPenaltyEntity.setAuditTime(new Date());
            admPenaltyService.update(oldAdmPenaltyEntity);
            s = JSON.toJSONString(oldAdmPenaltyEntity);
            sysAuditService.saveSuccessAudit("行政处罚审核成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_AUDIT, s, request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("行政处罚审核失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_AUDIT, s, e, request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 中国局审核行政处罚(批量审核)
     * taskStatus 通过 3 未通过4
     * 未通过添加未通过原因
     * 审核人 审核时间
     */
    @PostMapping("/batchaudit")
    public R batchaudit(@RequestParam String ids ,@RequestParam String auditflag,
                        @RequestParam(required = false) String auditReason){
      String s = "";
      try{
          if (com.alibaba.druid.util.StringUtils.isEmpty(ids)) {
              throw new RuntimeException("ids为空!");
          }
          //解析ids获取成idsList
          List<String> idList = Arrays.asList(ids.split(","));
          //遍历idsList
          for(String nowid : idList){
              AdmPenaltyEntity oldAdmPenaltyEntity = admPenaltyService.findById(nowid);
              if(auditflag.equals("3")){
                  oldAdmPenaltyEntity.setTaskStatus(Constant.ISSUBMIT_PASS);
              }else {
                  oldAdmPenaltyEntity.setTaskStatus(Constant.ISSUBMIT_NOTPASS);
              }
              if(!StringUtils.isEmpty(auditReason)) {
                  oldAdmPenaltyEntity.setAuditReason(auditReason);
              }else{
                  oldAdmPenaltyEntity.setAuditReason("");
              }
              oldAdmPenaltyEntity.setAuditUser(ServiceUtil.getLoginName(request));
              oldAdmPenaltyEntity.setAuditOrg(ServiceUtil.getOrgName(request));
              oldAdmPenaltyEntity.setAuditTime(new Date());
              admPenaltyService.update(oldAdmPenaltyEntity);
          }
          s = JSON.toJSONString(auditReason);
          sysAuditService.saveSuccessAudit("行政处罚审核成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_AUDIT, s, request);
          return R.ok();
      }catch (Exception e){
          e.printStackTrace();
          sysAuditService.saveFailAudit("行政处罚批量审核失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_AUDIT, s, e, request);
          return R.error(e.getMessage());
      }
    }
    /**
     * 中国局退回行政处罚
     * taskStatus 退回成功 2
     * applayrecall为1则将状态 状态设置为2
     * 退回人 退回时间
     */
    @PostMapping("/forreturn")
    public R forreturn(@RequestBody AdmPenaltyEntity admPenaltyEntity ){
        String s ="";
        try{
            AdmPenaltyEntity oldAdmPenaltyEntity = admPenaltyService.findById(admPenaltyEntity.getId());
            BeanUtil.copyBeanNotNullToBean(admPenaltyEntity, oldAdmPenaltyEntity);
            if(StringUtils.isEmpty(oldAdmPenaltyEntity.getApplyRecall())){

            }else if(oldAdmPenaltyEntity.getApplyRecall().equals("1")){
                oldAdmPenaltyEntity.setApplyRecall("2");
            }
            oldAdmPenaltyEntity.setTaskStatus(Constant.ISSUBMIT_BACK);
            oldAdmPenaltyEntity.setBackTime(new Date());
            admPenaltyService.update(oldAdmPenaltyEntity);
            s = JSON.toJSONString(oldAdmPenaltyEntity);
            sysAuditService.saveSuccessAudit("行政处罚退回成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_FORRETURN, s, request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("行政处罚退回失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_FORRETURN, s, e, request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 省局主动撤回行政处罚
     * taskStatus从1改为0
     * 更新时间进行更新
     * 修改人
     */
    @PostMapping("/recall")
    public R recall(@RequestBody AdmPenaltyEntity admPenaltyEntity ){
        String s = "";
        try{
            AdmPenaltyEntity oldAdmPenaltyEntity = admPenaltyService.findById(admPenaltyEntity.getId());
            BeanUtil.copyBeanNotNullToBean(admPenaltyEntity, oldAdmPenaltyEntity);
            oldAdmPenaltyEntity.setTaskStatus(Constant.ISSUBMIT_FALSE);
            oldAdmPenaltyEntity.setUpdateUser(ServiceUtil.getLoginName(request));
            oldAdmPenaltyEntity.setUpdateTime(new Date());
            oldAdmPenaltyEntity.setPositiveOrg(ServiceUtil.getOrgName(request));
            oldAdmPenaltyEntity.setPositiveUser(ServiceUtil.getLoginName(request));
            oldAdmPenaltyEntity.setPositiveReason(admPenaltyEntity.getPositiveReason());
            oldAdmPenaltyEntity.setPositiveTime(new Date());
            admPenaltyService.update(oldAdmPenaltyEntity);
            s = JSON.toJSONString(oldAdmPenaltyEntity);
            sysAuditService.saveSuccessAudit("撤回成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_RECALL, s, request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("撤回失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_RECALL, s, e, request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 省局申请撤回行政处罚
     * applyrecall状态为1
     * 填写申请撤回原因
     * taskStatus改为5表示审核通过后主动申请退回
     */
    @PostMapping("/applyrecall")
    public R applyrecall(@RequestBody AdmPenaltyEntity admPenaltyEntity ){
        String s ="";
        try{
            AdmPenaltyEntity oldAdmPenaltyEntity = admPenaltyService.findById(admPenaltyEntity.getId());
            BeanUtil.copyBeanNotNullToBean(admPenaltyEntity, oldAdmPenaltyEntity);
            oldAdmPenaltyEntity.setApplyRecall("1");
            oldAdmPenaltyEntity.setTaskStatus(Constant.ISSUBMIT_APPLYRECALL);
            oldAdmPenaltyEntity.setUpdateUser(ServiceUtil.getLoginName(request));
            oldAdmPenaltyEntity.setUpdateTime(new Date());
            oldAdmPenaltyEntity.setPositiveReason(admPenaltyEntity.getPositiveReason());
            oldAdmPenaltyEntity.setPositiveOrg(ServiceUtil.getOrgName(request));
            oldAdmPenaltyEntity.setPositiveUser(ServiceUtil.getLoginName(request));
            oldAdmPenaltyEntity.setPositiveTime(new Date());
            admPenaltyService.update(oldAdmPenaltyEntity);
            s = JSON.toJSONString(oldAdmPenaltyEntity);
            sysAuditService.saveSuccessAudit("撤回成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_RECALL, s, request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("撤回失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_RECALL, s, e, request);
            return R.error(e.getMessage());
        }
    }
}
